#include <QTcpServer>
#include "server.h"
#include "peer.h"

Server::Server (QObject * parent) : QTcpServer (parent)
{}

bool Server::startServer (const QString &serverAddress)
{
    QHostAddress addr (serverAddress);
    port = this->port;

    if (!listen (addr, port)) {
        return false;
    }

    connect(this, SIGNAL(newConnection()), host, SLOT(incomingConnection(nextPendingConnection())));
    return true;
}

bool Server::stopServer(QVector<Peer *> &peers)
{
    for (int i = 0; i < peers.count(); i++) {
        peers.at(i)->closeConnection();
    }
    peers.clear();
    close();
    if (isListening())
        return false;
    return true;
}

//void Server::acceptConnection()
//{
//    cpeer = new Connection(nextPendingConnection());
//}
